import { staffRotationCreate, staffRotationUpdate } from "@/api/SecondmentDictionary";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { useState } from "react";
import { useTranslation } from "react-i18next";
let getStaffRotation: Function;
let rotaId: string;
const timeFn = (tiem: string) => {
	const [date] = tiem.split(" ");
	return date;
};
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const formSchema: FormSchema[] = [
		{
			label: t("felix_ui.full_name"),
			field: "fullName",
			component: "Input"
		},
		{
			label: t("felix_ui.birthday"),
			field: "birthday",
			component: "DatePicker"
		},
		{
			label: t("felix_ui.gender"),
			field: "gender",
			component: "Select",
			labelWidth: 180,
			componentProps: {
				options: [
					{ label: t("kevin_ui.male"), value: 0 },
					{ label: t("kevin_ui.female"), value: 1 }
				]
			}
		},
		{
			label: t("felix_ui.poz_level"),
			field: "pozLevel",
			component: "InputNumber"
		},
		{
			label: t("felix_ui.date_from"),
			field: "dateFrom",
			component: "DatePicker"
		},
		{
			label: t("felix_ui.date_to"),
			field: "dateTo",
			component: "DatePicker"
		},
		{
			label: t("felix_ui.fieldOfImplementation"),
			field: "fieldOfImplementation",
			component: "Input",
			labelWidth: 180
		}
	];
	const [isAdd, setIsAdd] = useState(true);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 180,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		getStaffRotation = data.getStaffRotation;
		resetFields();
		const record = data.record || {};
		if (record) {
			rotaId = data.record.rotaId;
			setFieldsValue(record);
			setIsAdd(false);
		}
	});

	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = staffRotationCreate;
		console.log(formValue);
		if (!isAdd) {
			api = staffRotationUpdate;
		}
		formValue.birthday = timeFn(formValue.birthday);
		formValue.dateFrom = timeFn(formValue.dateFrom);
		formValue.dateTo = timeFn(formValue.dateTo);
		const res = await api({ ...formValue, rotaId });
		console.log(res);
		getStaffRotation();
		closeDrawer();
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
